<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Goods;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Zhusaidong\GridExporter\Exporter;

class ShopTopBookController extends Controller {
	use HasResourceActions;
	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('店铺销售排名')
			->description('某个店铺销售数据排名')
			->body($this->grid());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$shop_id = request()->get('shop_id');
		$grid = new Grid(new Goods);
		$grid->disableCreateButton();
		$grid->disableActions();
		$grid->model()->selectRaw("id,item_id,add_time,url,shop_id,isbn,img_url,date,areaname,pinxiang,title,count(title) as num,price,star")->where('shop_id', $shop_id)->groupBy('title')->orderBy('num', 'desc');

		$grid->id('ID');
		$grid->isbn('ISBN')->sortable();
		$grid->img_url('图片')->image(100, 100)->modal(function ($modal) {
			$big_img = str_replace("_s", "", $this->img_url);
			return "<img style='width:100%;' src='" . $big_img . "'>";
		});

		$grid->price('销售价格');
		$grid->add_time('上架时间');
		$grid->date('销售时间');
		$grid->areaname('销售地区');
		$grid->pinxiang('品相');
		$grid->title('商品名')->display(function ($title) {
			$url = route('goods.show', ['goods' => $this->getKey()]);
			return "<a target='_blank'  href='" . $url . "'>" . $title . "</a>";
		});
		$grid->price('价格');
		$grid->num('销量');
		$grid->url('孔网成交地址')->link();
		// $grid->shop_id('孔网店铺地址')->display(function ($shop_id) {
		// 	return "http://shop.kongfz.com/" . $shop_id;
		// })->link();
		$grid->cloumn('多抓鱼搜索')->display(function () {
			return "https://www.duozhuayu.com/search/" . $this->title;
		})->link();
		$states = [
			'on' => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
			'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
		];
		$grid->column('收藏')->switch();
		$exporter = Exporter::get($grid);
		$exporter->setFileName('店铺商品列表.xlsx');
		$exporter->setExclusions(['img_url', 'url']);

		return $grid;
	}

	protected function form() {
		$form = new Form(new Goods);
		$form->tools(function ($tools) {
			$tools->disableDelete();
		});
		$form->display('id');
		$form->text('title', '商品名');
		// $form->radio('star', '收藏')->options(['0' => '未收藏', '1' => '已收藏']);
		$states = [
			'on' => ['value' => 1, 'text' => '打开', 'color' => 'success'],
			'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
		];

		$form->switch('star', '收藏')->states($states);
		$form->display('price', '成交价格');
		$form->display('pinxiang', '品相');
		$form->display('date', '成交时间');
		$form->display('url', '孔网成交地址');
		$form->file('img_url', '商品图片');
		$form->starRating('rate', '商品评分');
		return $form;
	}

}
